﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using LevenBlog.Core.User;

namespace LevenBlog.Core.Mvc
{
    public abstract class AdminBaseController : BaseController
    {
        private AdminMembership membership = null;
        private static AdminViewEngine currentViewEngine;
        private static ViewEngineCollection adminViewEngineCollection = new ViewEngineCollection();
        private static object lockObject = new object();

        public AdminBaseController()
        {
            //viewLocator = new AdminViewLocator();
            //WebFormViewEngine viewEngine = (WebFormViewEngine)ViewEngine;
            //viewEngine.ViewLocator = viewLocator;
            if (currentViewEngine == null)
            {
                lock (lockObject)
                {
                    if (currentViewEngine == null)
                    {
                        currentViewEngine = new AdminViewEngine();
                        //ViewEngines.Engines.Add(currentViewEngine);
                        adminViewEngineCollection.Add(currentViewEngine);
                    }
                }
            }
        }

        public override IMembership Membership
        {
            get
            {
                if (membership == null)
                {
                    membership = new AdminMembership();
                    membership.Provider = Provider;
                    membership.Read();
                }
                return membership;
            }
        }

        protected IMembership UserMembership
        {
            get
            {
                return base.Membership;
            }
        }

        [NonAction]
        public ActionResult Message(string title, string content, object jumpRoute, string jumpUrl, MessageIcon icon)
        {
            MessageEntity message = new MessageEntity();
            message.Title = title;
            message.Content = content;
            message.JumpRoute = new RouteValueDictionary(jumpRoute);
            message.JumpUrl = jumpUrl;
            message.Icon = icon;
            ViewData.Model = message;
            return View("message");
        }

        [NonAction]
        public ActionResult Message(string title, string content, string url, MessageIcon icon)
        {
            MessageEntity message = new MessageEntity();
            message.Title = title;
            message.Content = content;
            message.JumpUrl = url;
            message.Icon = icon;
            ViewData.Model = message;
            return View("message");
        }

        [NonAction]
        public ActionResult Message(string title, string content, MessageIcon icon)
        {
            MessageEntity message = new MessageEntity();
            message.Title = title;
            message.Content = content;
            message.Icon = icon;
            message.JumpUrl = "<a href=\"javascript:history.back();\" title=\"返回\">返回</a>";
            ViewData.Model = message;
            return View("message");
        }

        [NonAction]
        protected override ViewResult View(string viewName, string masterName, object model)
        {
            ViewResult result = base.View(viewName, masterName, model);
            result.ViewEngineCollection = adminViewEngineCollection;
            //result.ViewEngineCollection
            //result.ViewEngine = currentViewEngine;
            return result;
        }
    }
}
